package com.zhiyou.driving.controller;

/**
 * @author by zhouyanchao
 * Created on 2017/10/30.
 * Descr:
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

@Controller
@RequestMapping("/ajax")
public class AjaxController {

	@RequestMapping("index")
	public String index(){

		return "ajax";
	}

//	@ResponseBody
	@RequestMapping("random")
	public String random(HttpServletResponse response){

		Random random = new Random();
		int i = random.nextInt();

		String s = "<h2><strong>随机数：</strong>"+i+"</h2>";
		try {
			response.setCharacterEncoding("utf-8");
			response.setHeader("ContentType","utf-8");
			response.getWriter().write(s);

		}catch (IOException e){

		}
		return null;
	}

	@ResponseBody
	@RequestMapping("json")
	public Object json(HttpServletResponse response){

		Random random = new Random();
		int i = random.nextInt();

		HashMap map = new HashMap();
		map.put("aa",true);
		map.put("msg","返回信息了");
		map.put("num",i);
//
//		String s = JSON.toJSONString(map);
//		System.out.println(s);

		return map;

		/*try {
			response.setCharacterEncoding("utf-8");
			response.setHeader("ContentType","utf-8");
			response.getWriter().write(s);

		}catch (IOException e){

		}
		return null;
		*/
	}

	@ResponseBody
	@RequestMapping("html")
	public String html(){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<h1>加载的html内容</h1>");
		stringBuffer.append("<h2>加载HTML内容</h2>");
		stringBuffer.append("<a href='http://www.baidu.com' target='_blank'>跳到百度</a>");

		System.out.println(stringBuffer.toString());

		return stringBuffer.toString();
	}

	@RequestMapping("html2")
	public String html2(){

		return "html";
	}


}
